package com.yellowpage.wf.action;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.dao.BaseDao;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.util.SpringContextUtil;
import com.yellowpage.wf.data.Workflow;
import com.yellowpage.wf.data.WorkflowDataUtils;
import com.yellowpage.wf.data.WorkflowInstance;
import com.yellowpage.wf.data.WorkflowNode;
import com.yellowpage.wf.data.WorkflowPath;
import com.yellowpage.wf.service.WorkflowInstanceService;
import com.yellowpage.wf.service.WorkflowNodeInstanceService;
import com.yellowpage.wf.service.WorkflowNodeService;
import com.yellowpage.wf.service.WorkflowService;

public class WorkflowInstanceAction extends BaseAction {

	public static final String RESULT_QUERY_ACTION = "query_action";
	public static final String INSTANCE_CONTROL = "instance_control";
	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<WorkflowInstance> workflowInstanceList = new ArrayList<WorkflowInstance>();
	private List<Map> resultList = new ArrayList<Map>();

	private WorkflowInstance workflowInstance;

	
	private WorkflowService workflowService;
	public WorkflowNodeService getWorkflowNodeService() {
		return workflowNodeService;
	}

	public void setWorkflowNodeService(WorkflowNodeService workflowNodeService) {
		this.workflowNodeService = workflowNodeService;
	}

	public WorkflowNodeInstanceService getWorkflowNodeInstanceService() {
		return workflowNodeInstanceService;
	}

	public void setWorkflowNodeInstanceService(
			WorkflowNodeInstanceService workflowNodeInstanceService) {
		this.workflowNodeInstanceService = workflowNodeInstanceService;
	}

	private WorkflowNodeService workflowNodeService;
	private WorkflowInstanceService workflowInstanceService;
	private WorkflowNodeInstanceService workflowNodeInstanceService;
	
	public WorkflowService getWorkflowService() {
		return workflowService;
	}

	public void setWorkflowService(WorkflowService workflowService) {
		this.workflowService = workflowService;
	}

	public WorkflowInstanceService getWorkflowInstanceService() {
		return workflowInstanceService;
	}

	public void setWorkflowInstanceService(
			WorkflowInstanceService workflowInstanceService) {
		this.workflowInstanceService = workflowInstanceService;
	}

	private String selectedId;
	private String[] selectedIds;
	private String message;
	private List<Workflow> workflowList;
	private Workflow workflow;
	private WorkflowNode workflowNode;
	private WorkflowPath workflowPath;

	private String query_workflowInstanceId;
	private String query_workflowId;
	private String query_workflowNodeId;
	private String query_createUser;
	private String query_createTime;
	private String query_lastUpdateUser;
	private String query_lastUpdateTime;
	private String query_owner;
	private String query_status;
	private List<Map> workflowNodeInstanceMapList;
	private Map workflowInstanceStatusMap;

	public Map getWorkflowInstanceStatusMap() {
		return workflowInstanceStatusMap;
	}

	public void setWorkflowInstanceStatusMap(Map workflowInstanceStatusMap) {
		this.workflowInstanceStatusMap = workflowInstanceStatusMap;
	}

	public Workflow getWorkflow() {
		return workflow;
	}

	public void setWorkflow(Workflow workflow) {
		this.workflow = workflow;
	}

	public WorkflowNode getWorkflowNode() {
		return workflowNode;
	}

	public void setWorkflowNode(WorkflowNode workflowNode) {
		this.workflowNode = workflowNode;
	}

	public WorkflowPath getWorkflowPath() {
		return workflowPath;
	}

	public void setWorkflowPath(WorkflowPath workflowPath) {
		this.workflowPath = workflowPath;
	}

	public List<Workflow> getWorkflowList() {
		return workflowList;
	}

	public void setWorkflowList(List<Workflow> workflowList) {
		this.workflowList = workflowList;
	}

	public void prepare() {
		super.prepare();
		workflowList = workflowService.getAllWorkflow();
		workflowInstanceStatusMap = WorkflowDataUtils
				.takeWorkflowInstanceStatusMap();
	}

	public String queryByPage() throws UnsupportedEncodingException {
		initPageRender();
		if (this.message != null && !message.equals("")) {
			addActionMessage(new String(message.getBytes("ISO-8859-1"), "UTF-8"));
		}
		Map queryMap = new HashMap();
		queryMap.put("workflowInstanceId", query_workflowInstanceId);
		queryMap.put("workflowId", query_workflowId);
		queryMap.put("workflowNodeId", query_workflowNodeId);
		queryMap.put("createUser", query_createUser);
		queryMap.put("createTime", query_createTime);
		queryMap.put("lastUpdateUser", query_lastUpdateUser);
		queryMap.put("lastUpdateTime", query_lastUpdateTime);
		queryMap.put("owner", query_owner);
		queryMap.put("status", query_status);

		resultList = workflowInstanceService.queryWorkflowInstanceByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		WorkflowInstance workflowInstance = new WorkflowInstance();
		workflowInstance.setWorkflowId(Long.valueOf(query_workflowId));
		workflowInstance.setStatus("on");
		workflowInstance.setCreateTime(new Date());
		workflowInstance.setCreateUser(loginUser.getUserId());
		workflowInstance.setLastUpdateTime(new Date());
		workflowInstance.setLastUpdateUser(loginUser.getUserId());
		try {
			workflowInstanceService.startWorkflowInstance(workflowInstance);
			message = "工作流启动成功!";
		} catch (Exception e) {
			message = "工作流启动失败!";
			addActionMessage(message);
			return RESULT_ADD;
		}
		;
		addActionMessage(message);

		return RESULT_QUERY_ACTION;
	}

	public void validateSave() {
		this.setValidatorResult("/wf/WorkflowInstanceAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);
		workflowInstance = workflowInstanceService.getWorkflowInstance(id);
		workflow = workflowService.getWorkflow(workflowInstance.getWorkflowId());
		Long nodeId = workflowInstance.getWorkflowNodeId();
		if (nodeId.intValue() > 0) {
			workflowNode = workflowNodeService.getWorkflowNode(nodeId);
		}
		workflowNodeInstanceMapList = workflowNodeInstanceService
				.getAllWorkflowNodeInstanceByInstanceId(workflowInstance
						.getWorkflowInstanceId());
		return RESULT_EDIT;
	}

	public List<Map> getWorkflowNodeInstanceMapList() {
		return workflowNodeInstanceMapList;
	}

	public void setWorkflowNodeInstanceMapList(
			List<Map> workflowNodeInstanceMapList) {
		this.workflowNodeInstanceMapList = workflowNodeInstanceMapList;
	}

	public String update() {
		try {
			workflowInstance = workflowInstanceService
					.getWorkflowInstance(workflowInstance
							.getWorkflowInstanceId());
			workflowInstance.setLastUpdateTime(new Date());
			workflowInstance.setLastUpdateUser(loginUser.getUserId());
			// demoWorkflow.instanceNext(workflowInstance);
			message = "工作流启动操作成功!";

		} catch (Exception e) {
			message = "工作流启动操作失败!";
		}
		;

		return INSTANCE_CONTROL;
	}

	public void validateUpdate() {
		this.setValidatorResult("/wf/WorkflowInstanceUpdate.jsp");
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setWorkflowInstance(WorkflowInstance workflowInstance) {
		this.workflowInstance = workflowInstance;
	}

	public WorkflowInstance getWorkflowInstance() {
		return workflowInstance;
	}

	public List<WorkflowInstance> getWorkflowInstanceList() {
		return workflowInstanceList;
	}

	public void setWorkflowInstanceList(
			List<WorkflowInstance> workflowInstanceList) {
		this.workflowInstanceList = workflowInstanceList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_workflowInstanceId() {
		return this.query_workflowInstanceId;
	}

	public void setQuery_workflowInstanceId(String workflowInstanceId) {
		this.query_workflowInstanceId = workflowInstanceId;
	}

	public String getQuery_workflowId() {
		return this.query_workflowId;
	}

	public void setQuery_workflowId(String workflowId) {
		this.query_workflowId = workflowId;
	}

	public String getQuery_workflowNodeId() {
		return this.query_workflowNodeId;
	}

	public void setQuery_workflowNodeId(String workflowNodeId) {
		this.query_workflowNodeId = workflowNodeId;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_lastUpdateUser() {
		return this.query_lastUpdateUser;
	}

	public void setQuery_lastUpdateUser(String lastUpdateUser) {
		this.query_lastUpdateUser = lastUpdateUser;
	}

	public String getQuery_lastUpdateTime() {
		return this.query_lastUpdateTime;
	}

	public void setQuery_lastUpdateTime(String lastUpdateTime) {
		this.query_lastUpdateTime = lastUpdateTime;
	}

	public String getQuery_owner() {
		return this.query_owner;
	}

	public void setQuery_owner(String owner) {
		this.query_owner = owner;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}

	public static void main(String[] args) throws Exception {
		ApplicationContext context = SpringContextUtil.getApplicationContext();
		BaseDao dao = (BaseDao) context.getBean("baseDao");
		Connection conn = dao.getConnection();
		System.out.println("over");
		Statement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "select * from ca_user ";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				System.out.println(rs.getString(1) + "||" + rs.getString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
